const { Script } = require("vm");

//需求：封装发送ajax请求的函数
function sendAjax(url, type = 'get', data = {}, dataType = 'json') {
    url = `http://49.235.224.225:3001${url}`;
    // url = `http://localhost:3000${url}`;

    return new Promise((resolve, reject) => {
        $.ajax({
            url,
            data,
            type,
            dataType,
            success(d) {
                //需求：返回d
                resolve([true, d]);
            },
            error(e) {
                resolve([false, e]);
            }
        });
    });

}

async function getUserinfo() {
    let jwtdata = localStorage['jwtdatas'];
    let [err, obj] = await sendAjax('/userinfo', 'get', {
        jwtdata,
    });
    console.log(obj, 888);
   
    if (err) {
        if (obj.status == 200) {
            $('nav .login').css({
                'display': 'none'
            });
            $('nav .user').css({
                'display': 'inline-block'
            });
            $('nav .user img').attr('src',`http://49.235.224.225:3001/${obj.data[0].head_photo_url}`)
        }
    } else {
        console.log('未查找到用户信息');
    }

    $('nav .user .out').click(function() {
        $('nav .login').css({
            'display': 'inline-block'
        });
        $('nav .user').css({
            'display': 'none'
        });
        localStorage.removeItem('jwtdatas');
    })

}



// 搜索
function showSearch(){
    let pages = 1;
         $('.search img').click(async function(){
            
            searchData();
           
        });

        async function searchData(){
            let keyword = $('.search input').val();
            // console.log(keyword);
            let [err,obj] = await sendAjax('/list/search','get',{
                keyword,
                pages,
            });

            console.log(obj,888);
            if (err) {
                   
                   let str = '';
                   let arr = obj.data;    
                   
                   show = `搜索到 <span>${keyword}</span> 关键字共<span>${arr[0]}</span> 条，共<span>${Math.ceil(arr[0]/3)}</span>页`;
                   $('.search_keyword').html(show);
                   for (i=1;i<arr.length;i++){
                       str += `<li>
   
                           <a href="./videoDetail.html?cid=${arr[i].cid}">
                               <div class="top">
                                  
                                   <img src="${arr[i].image_src}" alt="" class="m">
                                   <p>学科：${arr[i].subject_name}</p>
                               </div>
                               <div class="bottom">
                                   <div class="left">
                                       <span>${arr[i].title}</span>
                                       <span class="time">地区：${arr[i].area_name}</span>
                                   </div>
                                   <div class="right">    
                                           ${arr[i].price==0 ? '免费学习' : arr[i].price+'元' } 
                                       </div>
                               </div>
                               </a>
                           </li>`;
                   }             
                   $('main .lesson>ul').html(str);
                   showPage(arr[0]);
               } else {
                   console.log('查找数据接口调用失败');
               }
            
        }
       
        let num = 0;
        function  showPage(n){ 
            let p ="";
            num = Math.ceil(n/3);
            for(j=1; j<=num; j++){

                if(j==pages){
                    p += `<li class='active'>${j}</li>`;
                }else{
                    p += `<li>${j}</li>`;
                }
            }
               
                $('.pageNum ul').html(p);
            
                $('.pageNum ul li').click(function(){
                    pages = $(this).text();
                    searchData();
                })
        }


        
        getUserinfo();
 }
